package com.sbck.cs3.fragment;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.sbck.cs3.MyGlobal;
import com.sbck.cs3.MyClass.MyMessage;
import com.sbck.cs3.R;
import com.sbck.cs3.MyClass.TChState;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class FireFragment extends Fragment {

    Button buttonFire;
    Button buttonChange;
    Button buttonStatus;
    View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fire, container, false);

        buttonFire = view.findViewById(R.id.Btn_Fire_Fire);
        buttonChange = view.findViewById(R.id.Btn_Fire_Change);
        buttonStatus = view.findViewById(R.id.Btn_Fire_Status);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //EventBus
        if(!EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().register(this);
        }

        this.view = view;

        String msg = "下发焰条巡检指令...";
        MyGlobal.socketUtil.SendCmd_FireStatus();
        ShowToast(msg);
        MyGlobal.AddLog(msg,Color.DKGRAY);

        ButtonClick();
    }


    private void ButtonClick()
    {
        //点火按钮
        buttonFire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg = "下发焰条点火指令...";
                MyGlobal.socketUtil.SendCmd_Fire();
                ShowToast(msg);
                MyGlobal.AddLog(msg,Color.DKGRAY);
            }
        });

        //更换焰条
        buttonChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg = "下发更换焰条指令...";
                MyGlobal.socketUtil.SendCmd_FireReset();
                ShowToast(msg);
                MyGlobal.AddLog(msg,Color.DKGRAY);
            }
        });

        //焰条巡检
        buttonStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String msg = "下发焰条巡检指令...";
                MyGlobal.socketUtil.SendCmd_FireStatus();
                ShowToast(msg);
                MyGlobal.AddLog(msg,Color.DKGRAY);
            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if(EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().unregister(this);
        }
    }

    private void ShowToast(String msg)
    {
        if(MyGlobal.IsPopToast.get())
            Toast.makeText(getActivity(),msg , Toast.LENGTH_SHORT).show();
    }

    //事件总线
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MyMessage myMessage) {
        switch (myMessage.messageCategory) {
            case SocketReceive:
                break;
            case SocketSend:
            case SocketError:
                return;
        }

        switch (myMessage.messageType) {
            case FireLight:
            case FireReset:
            case FireState:
                ListView lv_list = view.findViewById(R.id.LV_Fire_list);
                lv_list.setAdapter(new MyAdapter());
                break;
        }

    }
    private class MyAdapter extends BaseAdapter
    {
        int count;
        public  MyAdapter()
        {

            count = MyGlobal.tChStateArrayList.size();
        }
        @Override
        public int getCount() {

            return  count;

        }

        @Override
        public Object getItem(int i) {
            return MyGlobal.tChStateArrayList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            View v1;
            if(view == null)
            {
                v1 = View.inflate(getActivity(),R.layout.item_fire,null);
            }
            else
            {
                v1 = view;
            }
            LinearLayout linearLayout = v1.findViewById(R.id.LL_ItemFire_BackGround);
            TextView tvIndex =  v1.findViewById(R.id.TV_ItemFire_Index);
            TextView tvStatus =  v1.findViewById(R.id.TV_ItemFire_Status);

            TChState tChState =  MyGlobal.tChStateArrayList.get(i);


            if(tChState.Status == 0)
            {
                tvStatus.setBackgroundColor(Color.parseColor("#a1afc9"));
            }
            else if(tChState.Status == 1)
            {
                tvStatus.setBackgroundColor(Color.parseColor("#003472"));
            }
            else if(tChState.Status == 2)
            {
                tvStatus.setBackgroundColor(Color.parseColor("#ff7500"));
            }
            else if(tChState.Status == 3)
            {
                tvStatus.setBackgroundColor(Color.parseColor("#3d3b4f"));
            }
            else if(tChState.Status == 4)
            {
                tvStatus.setBackgroundColor(Color.parseColor("#9d2933"));
            }
            tvIndex.setText(String.valueOf( tChState.Index));
            tvStatus.setText(tChState.GetFireStatus());
            return v1;
        }
    }
}
